package com.twu.biblioteca;

import com.twu.biblioteca.model.BookDAO;
import com.twu.biblioteca.model.MovieDAO;

import java.util.ArrayList;
import java.lang.Exception;

/**
 * Created by ggoulart on 9/18/14.
 */
public class Biblioteca {

    public ArrayList<Book> listBooks() {
        BookDAO bookDAO = new BookDAO();
        return bookDAO.getAvailableBooks();
    }

    public String checkoutBook(String bookName) {
        try {
            BookDAO bookDAO = new BookDAO();
            Book book = bookDAO.findBookByName(bookName);
            if(bookDAO.removeAvailableBook(book))
                bookDAO.addCheckoutBooks(book);
            else
                throw new Exception();
            return "Thank you! Enjoy the book.";
        } catch (Exception e) {
            return "That book is not available.";
        }
    }

    public String returnBook(String bookName) {
        try {
            BookDAO bookDAO = new BookDAO();
            Book book = bookDAO.findBookByName(bookName);
            if(bookDAO.removeCheckoutBooks(book))
                bookDAO.addAvailableBook(book);
            else
                throw new Exception();
            return "Thank you for returning the book.";
        } catch (Exception e) {
            return "That is not a valid book to return.";
        }
    }

    public ArrayList<Movie> listMovies() {
        MovieDAO movieDAO = new MovieDAO();
       return movieDAO.getAvailableMovies();
    }

    public String checkoutMovie(String movieName) {
        try {
            MovieDAO movieDAO = new MovieDAO();
            Movie movie = movieDAO.findMovieByName(movieName);
            if(movieDAO.removeAvailableMovie(movie))
                movieDAO.addCheckoutMovie(movie);
            else
                throw new Exception();
            return "Thank you! Enjoy the movie.";
        } catch (Exception e) {
            return "That movie is not available.";
        }
    }

}
